import axios from "axios";
class AjaxRequest{
    constructor(){
        this.baseURL = process.env.NODE_ENV !=="production"?"http://localhost:9900/api":"/";
        this.timeout = 4000
    }
    setInterceptor(instance,url){
        // 请求的拦截
        instance.interceptors.request.use(config=>{
            config.headers.token = localStorage.getItem('token') || ''
            return config;
        },err=>{
            Promise.reject(err)
        }),
        // 响应的拦截
        instance.interceptors.response.use(res=>{
            if(res.data.code === 0){
                return res.data.data;
            }else{
                return Promise.reject(res.data)
            }
        },err=>{
            Promise.reject(err)
        })
    }
    request(options){
        const instance = axios.create();
        let config = {
            baseURL:this.baseURL,
            timeout:this.timeout,
            ...options
        }
        this.setInterceptor(instance,options.url);// 设置请求拦截
        return instance(config)
    }

}
export default new AjaxRequest();